package com.data.modules.gd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.modules.gd.entity.GdEventLog;
import com.data.modules.gd.mapper.GdEventLogMapper;
import com.data.modules.gd.service.IGdEventLogService;
import com.data.modules.gd.vo.CityRankVo;
import com.data.modules.gd.vo.DataVo;
import com.data.modules.gd.vo.EventLogVo;
import com.data.modules.gd.vo.TvRankVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 新闻屏事件日志
 * @Author: data-boot
 * @Date:   2023-08-29
 * @Version: V1.0
 */
@Service
public class GdEventLogServiceImpl extends ServiceImpl<GdEventLogMapper, GdEventLog> implements IGdEventLogService {

    @Autowired
    private GdEventLogMapper mapper;
    @Override
    public void addEventLog(EventLogVo eventLogVo) {
        GdEventLog event = new GdEventLog();
        BeanUtil.copyProperties(eventLogVo,event);
        this.save(event);
    }

    @Override
    public IPage<GdEventLog> queryEvnetPageList(Page<GdEventLog> page, GdEventLog gdEventLog) {

        return mapper.queryEvnetPageList(page,gdEventLog);
    }

    @Override
    public List<TvRankVo> getSevenAvgTime() {

        List<TvRankVo> newList = new ArrayList<>();
        List<TvRankVo> list =mapper.searSevenAvgTime();

        list.stream().forEach(x->{
            TvRankVo vo = new TvRankVo();
            vo.setType(x.getType());
            vo.setLongTime(x.getLongTime());
            vo.setNum(x.getNum());
            // 为了展示好看 除60 调整为 除6000
            BigDecimal cost = new BigDecimal(x.getLongTime()).divide(new BigDecimal(6000),2,BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(x.getNum()),2,BigDecimal.ROUND_HALF_UP);

            vo.setAvgCost(cost);
            newList.add(vo);
        });

        return newList.stream().sorted(Comparator.comparing(TvRankVo::getAvgCost).reversed()).collect(Collectors.toList());
    }
}
